package com.controller;

import com.model.FlightModel;
import com.services.IFlightServices;
import com.services.impl.FlightServicesImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Program: exam
 * @ClassName: PageFlightServletController
 * @Description: 分页加模糊查询Flight
 * @Author: li
 * @Create: 2019-07-24 20:03
 */
@WebServlet("/flight/list")
public class PageFlightServletController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        Map<String, Object> result = new HashMap<>();
        
        try {
            IFlightServices FlightServices = new FlightServicesImpl();
            
            // 获取所有航班数据
            List<FlightModel> flightModels = FlightServices.queryAllFlight();
            
            // 返回结果
            result.put("code", 0);
            result.put("msg", "success");
            result.put("data", flightModels);
            
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "获取航班列表失败：" + e.getMessage());
        }
        
        response.getWriter().write(JSONObject.fromObject(result).toString());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
